
import java.util.regex.Pattern;

public class Person {
    protected String name;
    protected char sex;
    protected String phoneNum;
    protected String PID;
    protected String PWD = "oms1921SE";

    public void setName(String name) {
        this.name = name;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }

    public void setPID(String pID) {
        PID = pID;
    }

    public void setPWD(String pWD) {
        PWD = pWD;
    }

    public String getName() {
        return name;
    }

    public char getSex() {
        return sex;
    }

    public String getPID() {
        return PID;
    }

    public String getPWD() {
        return PWD;
    }

    public String getPhoneNum() {
        return phoneNum;
    }

    public void changePWD(String newPWD, String confirmNewPWD) {
        if (!checkPWD(newPWD)) {
            System.out.println("New password illegal");
        } else if (!newPWD.equals(confirmNewPWD)) {
            System.out.println("Not match");
        } else {
            this.setPWD(newPWD);
            System.out.println("Change password success");
        }
    }

    public void thisInfo() {
        System.out.println("[info]\n" + "| name:\t" + name + "\n" + "| Sex:\t" + sex + "\n" + "| Pho:\t" + phoneNum
                + "\n" + "| PID:\t" + PID + "\n" + "| Pwd:\t" + PWD + "\n" + "| Type:\t" + this.getClass().getName());

    }

    public static boolean checkSex(String sexString) {
        if (sexString.length() != 1)
            return false;
        char sex = sexString.charAt(0);
        if (sex == 'M' || sex == 'F') {
            return true;
        }
        return false;
    }

    public static boolean checkNum(String phoneNum) {
        int len = phoneNum.length();
        if (len != 11)
            return false;
        for (int i = 0; i < len; i++)
            if (!Character.isDigit(phoneNum.charAt(i)))
                return false;
        int part1 = Integer.parseInt(phoneNum.substring(0, 3));
        int part2 = Integer.parseInt(phoneNum.substring(3, 7));
        int part3 = Integer.parseInt(phoneNum.substring(7, 10));
        int part4 = Integer.parseInt(phoneNum.substring(10, 11));

        if ((part1 >= 130 && part1 <= 187) && (part2 >= 0 && part2 <= 9999) && (part3 >= 31 && part3 <= 71)
                && (part4 == 0 || part4 == 1)) {
            return true;
        }
        return false;

    }

    public static boolean checkName(String name) {

        String regex = "[0-9a-zA-Z]+";
        Pattern pattern = Pattern.compile(regex);
        return pattern.matcher(name).matches();

    }

    // 创建一个person，若数据格式错误则返回null
    public static Person newPerson(String name, String sexString, String phoneNum) {

        Person tmpPerson = new Person();
        if (checkSex(sexString) && checkNum(phoneNum)// 合法的条件下检查性别与尾号是否匹配
                && ((sexString.charAt(0) == 'M' && phoneNum.charAt(10) == '0')
                        || (sexString.charAt(0) == 'F' && phoneNum.charAt(10) == '1'))) {
            tmpPerson.name = name;
            tmpPerson.phoneNum = phoneNum;
            tmpPerson.sex = sexString.charAt(0);
            return tmpPerson;
        } else {
            if (!checkSex(sexString)) {
                System.out.println("Sex illegal");
            } else {
                System.out.println("Phone number illegal");
            }
            return null;
        }
    }

    public static Person newSudoPerson(String name, String sexString, String phoneNum, String type) {
        Person tmpPerson;
        switch (type) {
        case MyUtil.SUDO_CUSTOMER:
            tmpPerson = new Customer();
            break;
        case MyUtil.SUDO_WAITER:
            tmpPerson = new Waiter();
            break;
        case MyUtil.SUDO_COOK:
            tmpPerson = new Cook();
            break;
        default:
            tmpPerson = new Person();
            break;
        }
        if (!checkSex(sexString)) {
            System.out.println("Sex illegal");
        } else if (!checkNum(phoneNum)) {
            System.out.println("Phone number illegal");
        } else if ((sexString.charAt(0) == 'M' && phoneNum.charAt(10) == '1')
                || (sexString.charAt(0) == 'F' && phoneNum.charAt(10) == '0')) {
            System.out.println("Phone number doesn't match sex");
        } else {
            tmpPerson.name = name;
            tmpPerson.phoneNum = phoneNum;
            tmpPerson.sex = sexString.charAt(0);
            return tmpPerson;
        }
        return null;
    }

    public boolean checkPID(String str,String type) {
        String regex;
        switch (type){
            case MyUtil.SUDO_CUSTOMER:
                regex = "(Cu)\\d{5}";
                break;
            case MyUtil.SUDO_COOK:
                regex = "(Co)\\d{5}";
                break;
            case MyUtil.SUDO_WAITER:
                regex = "(Wa)\\d{5}";
                break;
            case "ALL":
                regex = "(Cu|Wa|Bo|Co)\\d{5}";
                break;
            default:
                regex = "(Cu|Wa|Bo|Co)\\d{5}";
                break;
        }
        Pattern pattern = Pattern.compile(regex);
        return pattern.matcher(str).matches();
    }

    public static boolean checkPWD(String str) {
        String regex = "(?=.*([a-zA-Z].*))(?=.*[0-9].*)[\\x21-\\x7E]{8,18}$";
        Pattern pattern = Pattern.compile(regex);
        return pattern.matcher(str).matches();
    }

    public String toString() {
        return String.format("Name:%s%nSex:%c%nPhone:%s", name, sex, phoneNum);

    }
}
